/*
 * cli.c
 *
 *  Created on: Sep 11, 2013
 *      Author: Facu
 */


#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
/*****************************************************************************
 *   History
 *   2010.07.01  ver 1.01    Added support for UART3, tested on LPCXpresso 1700
 *   2009.05.27  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "type.h"
#include "uart.h"
#include <string.h>
#include <stdlib.h>
#include "error.h"

//#define ERR1 "Fuera de Rango\r\n"
//#define ERR2 "Comando no Valido\r\n"

extern volatile uint32_t UART3Count;
extern volatile uint8_t UART3Buffer[BUFSIZE];
extern volatile uint8_t indice;
void rgb_set(uint8_t a, char color);



/*-----------------------------------------------------------------------------
  Lector de Comandos - INTERPRETE
*----------------------------------------------------------------------------*/

void procesar_comandos(uint8_t d){
	switch(d){

	case 0x52:  //Encender Led Rojo
		LPC_GPIO2->FIOSET=(1<<4);
		LPC_GPIO2->FIOCLR=(1<<3);
		break;
	case 0x42: //Encender Led Azul
		LPC_GPIO2->FIOSET=(1<<3);
		LPC_GPIO2->FIOCLR=(1<<4);

	default:
		break;
	}

}

void process_debugcmd(char color){

	uint8_t lenght=0;
	uint8_t numero;
	uint8_t flagError=0;
	indice--;
	//UART3Buffer[indice]; //Aca esta la 'R'
	while(UART3Buffer[indice] != '.'){
		indice --;
		lenght++; //Contador BREAK o contador caracteres
		if(lenght>3){ //No llego numero o llega mal
			flagError=1;
			break;
		}
	}
		if(flagError == 0){
		switch(lenght){

			case 1:  // .2R]
				numero = UART3Buffer[indice+1] - '0';

				if(numero >= 0 && numero <= 255){
				rgb_set(numero,color);
				}else{
					UARTSend(3, (uint8_t *)ERR1 , strlen(ERR1) ); //Fuera de Rango
					}
				break;
			case 2: // .20R]
				numero = (UART3Buffer[indice+1] - '0')*10 +  (UART3Buffer[indice+2] - '0');

				if(numero >= 0 && numero <= 255){
					rgb_set(numero,color);
					}else{
					UARTSend(3, (uint8_t *)ERR1 , strlen(ERR1) ); //Fuera de Rango
					}
				break;

			case 3: // .200R]
				numero = (UART3Buffer[indice+1] - '0')*100 +  (UART3Buffer[indice+2] - '0')*10 +(UART3Buffer[indice+3] - '0') ;
				if(numero >= 0 && numero <= 255){
					rgb_set(numero,color);
					}else{
					UARTSend(3, (uint8_t *)ERR1 , strlen(ERR1) ); //Fuera de Rango
					}
				break;

			default:
				UARTSend(3, (uint8_t *)ERR2 , strlen(ERR2) );
				break;
			}
	}else{
		UARTSend(3, (uint8_t *)ERR2 , strlen(ERR2) );
	}
}
